#include "scr_state_credits.h"
#include "scr_state_main_menu.h"
#include "scr_game.h"
#include "scr_game_state_manager.h" 
#include "scr_texture.h"
#include "scr_font.h"

#define CREDITS_X 40
#define CREDITS_Y SCRGame::SCR_SCREEN_HEIGHT
#define CREDITS_SPACE 160
#define CREDITS_NAME_SPACE 64

SCRStateCredits::SCRStateCredits()
	: m_Time(0)
	, m_IsPressed(false)
{
}

void SCRStateCredits::OnInitialize()
{
	m_Font0 = m_pGame->GetResourceManager()->GetFont("fonts/appleberry_with_cyrillic.ttf", 38);
	m_Font1 = m_pGame->GetResourceManager()->GetFont("fonts/carbon.ttf", 28);
	m_Image = m_pGame->GetResourceManager()->GetTexture("images/credits.png");
}

void SCRStateCredits::OnUpdate(const int& Delta)
{
	m_Time += Delta;
}

void SCRStateCredits::OnRender(const int& Delta)
{
	m_Image->Draw(0, 0);

	const char* Texts[] =
	{
		"Programmed By",
		"Roderick Rivero",
		"In-game Music By",
		"Erika Mariel Rivero",
		"Sprites By",
		"Roderick Rivero",
		"Paintings By",
		"Saul Espinosa, artofsaul.blogspot.com",
		"Fonts From",
		"www.1001freefonts.com"
	};

	int TextElements = sizeof(Texts) / sizeof(Texts[0]);
	int OffsetY = m_Time >> 3;

	for (int TextIdx = 0; TextIdx < TextElements; TextIdx += 2)
	{
		const int PosY = CREDITS_Y + (TextIdx * CREDITS_SPACE) - OffsetY;
		
		if (PosY < SCRGame::SCR_SCREEN_HEIGHT &&
			PosY > -CREDITS_SPACE)
		{
			m_Font1->SetColor(255, 0, 0);
			m_Font1->Draw(CREDITS_X, PosY, Texts[TextIdx]);
			m_Font0->SetColor(255, 255, 255);
			m_Font0->Draw(CREDITS_X, PosY + CREDITS_NAME_SPACE, Texts[TextIdx + 1]);
		}
	}

	if (OffsetY > (TextElements * CREDITS_SPACE) + SCRGame::SCR_SCREEN_HEIGHT)
	{
		m_Time = 0;
	}
}

void SCRStateCredits::OnTerminate()
{
}

void SCRStateCredits::OnKeyClick(const int& Key)
{
	if (!m_IsPressed)
	{
		m_IsPressed = true;
		m_pGame->GetStateManager()->SwitchTo<SCRStateMainMenu>();
	}	
}

void SCRStateCredits::OnMouseClick(const int& X, const int& Y, const SCRMouseButton::Enum& Button)
{
	if (!m_IsPressed)
	{
		m_IsPressed = true;
		m_pGame->GetStateManager()->SwitchTo<SCRStateMainMenu>();
	}	
}
